package ll.sms.dao;

import java.util.ArrayList;
import java.util.List;

import ll.sms.db.SqlBuilder;
import ll.sms.db.Transaction;
import ll.sms.exceptions.SmsDBException;
import ll.sms.pojo.SmsUser;
import ll.sms.util.SmsUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class SmsUserDAO extends BaseDAO {
	private static Log log = LogFactory.getLog(SmsUserDAO.class);

	public void saveUser(SmsUser user) {
		// uids, uuname, upwd, uphone,ucompany, usmsnum, uregisttime,uavailable

		try {

			String sql = SqlBuilder.getSql("SAVE_USER",
					user.getUids(),
					user.getUuname(),
					user.getUpwd(),
					user.getUphone(),
					user.getUcompany(),
					user.getUsmsnum(),
					user.getUregisttime(),// DATE
					user.getUavailable(),
					user.getUchannel(),
					user.getUlinkman(),
					user.getUnote()
					);
			this.update(sql, getConnection());
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			throw new SmsDBException(e);

		}

	}

	public int updatePropertyByUname(String fieldName, Object value,
			String uname) {
		try {
			String sql = SqlBuilder.getSql("UPDATE_BY_USER", SqlBuilder.toPropertyString(fieldName), value,
					uname);
			return this.update(sql, getConnection());
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			throw new SmsDBException(e);

		}

	}

	public int updateUser(SmsUser user){
		 try {
			 String sql = SqlBuilder.getSql("UPDATE_USER",
						user.getUuname(),
						user.getUpwd(),
						user.getUphone(),
						user.getUcompany(),
						user.getUsmsnum(),
						user.getUregisttime(),// DATE
						user.getUavailable(),
						user.getUchannel(),
						user.getUlinkman(),
						user.getUnote(),
						user.getUids()
						);

				return this.update(sql, getConnection());
		 } catch (Exception e) {
		 e.printStackTrace();
		 log.error(e);
		 throw new SmsDBException(e);

		 }

	}

	public List<SmsUser> findUserByProperties(String fieldName, Object value) {
		try {
			String sql = SqlBuilder.getSql("QUERY_USER_BY_PROPERTY", SqlBuilder.toPropertyString(fieldName),
					value);

			ArrayList<SmsUser> list = this.searchToBeanList(getConnection(),
					SmsUser.class, sql);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			throw new SmsDBException(e);

		}
	}

	public SmsUser findUserByIdOrUname(String fieldName, String value){
		List<SmsUser> list=findUserByProperties(fieldName, value);
		if(list==null || list.size()==0){
			return null;
		}
		return list.get(0);
	}


	public int deleteUserByProperty(String fieldName, Object value){
		 try {
			 String sql = SqlBuilder.getSql("DELETE_USER_BY_PROPERTY", SqlBuilder.toPropertyString(fieldName),
						value);
				return this.update(sql, getConnection());
		 } catch (Exception e) {
		 e.printStackTrace();
		 log.error(e);
		 throw new SmsDBException(e);

		 }

	}

	public List<SmsUser> findAllUsers(){
		 try {
			 String sql = SqlBuilder.getSql("QUERY_ALL_USERS");
			 return this.searchToBeanList(getConnection(), SmsUser.class, sql);
		 } catch (Exception e) {
		 e.printStackTrace();
		 log.error(e);
		 throw new SmsDBException(e);

		 }
	}


	public int countByUname(String username){
		 try {
			String sql = "select count(auname) from sms_admin where auname= '"+username+"'";
			Object[] a =  this.searchToArray(sql, getConnection());;
			if(a==null || a.length!=1){
				return -1;
			}
			return SmsUtil.toInt(a[0]);

		 } catch (Exception e) {
		 e.printStackTrace();
		 log.error(e);
		 throw new SmsDBException(e);
		 }
	}

	public String getUserDefaultChannel(String userName){

		 try {

			 String sql = SqlBuilder.getSql("QUERY_USER_CHANNEL",userName);
			 Object[] o = this.searchToArray(sql, getConnection());
			 if(o==null || o.length==0){
				 return null;
			 }
			 return SmsUtil.toStr(o[0]);

			 } catch (Exception e) {
			 e.printStackTrace();
			 log.error(e);
			 throw new SmsDBException(e);

			 }
		}
	public static void main(String[] args) {
		Transaction.begin();
		/*SmsUser u = new SmsUser();
		u.setUids(SmsUtil.getPrimaryKey());
		//System.out.println(u.getUids().length());
		u.setUavailable(1);
		u.setUcompany("a3a6615e575c414aa3198dd3cbbb0a44");
		u.setUphone("878988");
		u.setUpwd("123456");
		u.setUuname("maodie");
		u.setUsmsnum(20);
		u.setUregisttime(new Timestamp(new Date().getTime()));*/
		//System.out.println(new SmsUserDAO().countByUname("maodie12"));
		//System.out.println(list.size());
		String s = new SmsUserDAO().getUserDefaultChannel("sky");
		System.out.println(s);
		Transaction.commit();
	}

}
